import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.Timer;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;


public class gameClient extends Class1{
	Client cl = new Client();

	boolean con = false;

	boolean paused = false;

	boolean dead = false;

	boolean gotID = false;

	public gameClient(String ip)
	{
		super();

		rectangle = new rect(10, 1, 50, 50, 10); // x, y, width, height, speed
		rectangle2 = new rect(500, 450, 30, 30, 4);

		t = new Timer(60, this);
		t.start();

		dataT = new Timer(60, this);
		dataT.start();

		Kryo k = cl.getKryo();
		k.register(rect.class);
		k.register(missile.class);
		k.register(Rectangle2D.Double.class);
		k.register(Ellipse2D.Double.class);
		k.register(char[].class);
		k.register(circle.class);
		k.register(circle.class);

		cl.start();

		final Class1 c = this;
		cl.addListener(new Listener() {
			public void received (Connection connection, Object object) {
				if (object instanceof rect) {
					rect request = (rect)object;
					if(request.id == 0)
					{
						if(request.paused == true && paused == false)
						{
							t.stop();
							paused = true;
						}
						if(request.paused == false && paused == true)
						{
							t.start();
							paused = false;
						}

						rectangle = request;

						System.out.println(">>Good rectangle received from server<<");
					}
					else
					{
						badRect.put(request.hashCode(), request);
					}   
				}

				if(object instanceof circle){
					circle c = (circle) object;
					circles.put(c.hashCode(), c);

					System.out.println(">>Circle " + c.hashCode() + " received from server<<");
				}

				if(object instanceof String){
					String request = (String)object;
					if(request.equals("WIN") && won == false){
						won = true;
					}
					else if(request.contains("X"))
					{
						String[] newS = request.split("X");
						frame.setSize(Integer.parseInt(newS[0]), Integer.parseInt(newS[1]));
					}
					else if(request.contains("INGAMEMSG:"))
					{
						request = request.replace("INGAMEMSG: ", "");
						currentMessage = request;
					}
					else if(isInteger(request))
					{
						int id = Integer.parseInt(request);
						rectangle2.id = id;
						System.out.println(">>>>ASSIGNED ID " + id);
						dataT.start();
					}
				}

				if(object instanceof missile){
					missile m = (missile) object;
					missiles.put(m.hashCode(), m);
				}

			}
		});
		try {
			cl.connect(5000, ip, 12345);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		con = true;
	}



	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
			rectangle2.right = true;
			System.out.println("right");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
			rectangle2.left = true;
			System.out.println("left");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_UP) {
			rectangle2.up = true;
			System.out.println("up");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
			rectangle2.down = true;
			System.out.println("down");
		}
	}

	public void keyReleased(KeyEvent arg0)
	{
		if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
			rectangle2.right = false;
			System.out.println("right");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
			rectangle2.left = false;
			System.out.println("left");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_UP) {
			rectangle2.up = false;
			System.out.println("up");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
			rectangle2.down = false;
			System.out.println("down");
		}
		if(arg0.getKeyCode() == KeyEvent.VK_V)
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("PLEASE INPUT THE GAME MESSAGE TO BROADCAST: ");
			try {
				currentMessage = br.readLine();
				cl.sendTCP("INGAMEMSG: " + currentMessage);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println(e.getMessage());
			}
			t.start();
			rectangle.paused = false;
		}
		if(arg0.getKeyCode() == KeyEvent.VK_ENTER && won == true)
		{
			won = false;

			dead = false;
			
			rectangle.x = 10;
			rectangle.y = 50;

			rectangle2.x = 500;
			rectangle2.y = 450;

			this.repaint();
			t.start();
			dataT.start();
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource().equals(t))
		{
			if(won == true)
			{
				t.stop();
				dataT.stop();
				won = true;
				System.out.println("Rectangle intersected rectangle2 *WIN*");
			}
			if(rectangle.rectObject.intersects(rectangle2.rectObject))
			{
				t.stop();
				dataT.stop();
				won = true;
				System.out.println("Rectangle intersected rectangle2 *WIN*");
			}

			for(missile m: missiles.values())
			{
				if(rectangle2.rectObject.intersects((m.rectObject)))
				{
					dead = true;
					dataT.stop();
				}
			}

			//			if(circles.get(this.hashCode()) != null)
			//			{
			//				Ellipse2D.Double circObject = circles.get(this.hashCode()).circObject;
			//				double radius = circObject.width/2;
			//
			//				double distFromCenter = Math.hypot(this.rectangle2.rectObject.x - circObject.getCenterX(), this.rectangle2.rectObject.y - circObject.getCenterY());
			//				
			//				if(distFromCenter <= radius)
			//				{
			//					dead = true;
			//				}	
			//			}
			if(panel != null)
			{
				rectangle.update(panel.getWidth(), panel.getHeight());
				if(!dead)
				{
					rectangle2.update(panel.getWidth(), panel.getHeight());
				}
				for(rect r: badRect.values())
				{
					if(r.hashCode() != rectangle2.hashCode())
					{
						r.update(panel.getWidth(), panel.getHeight());
					}
				}
			}
		}
		if(arg0.getSource().equals(dataT))
		{
			if(con)
			{
				System.out.println(">>Bad Rectangle sent to Server<<");
				cl.sendTCP(rectangle2);
			}
			if(!gotID)
			{
				System.out.println(">>>>SENT ID REQUEST");
				cl.sendTCP("GETID");
				gotID = true;
			}
			if(currentMessage != null && !currentMessage.equals(""))
			{
				cl.sendTCP(currentMessage);
			}
		}


		this.repaint();	
	}

	public static boolean isInteger(String s) {
		try { 
			Integer.parseInt(s); 
		} catch(NumberFormatException e) { 
			return false; 
		}
		// only got here if we didn't return false
		return true;
	}
}

